package org.ourgrid.acceptance.discoveryservice;

import java.util.LinkedList;
import java.util.List;

import org.junit.Test;
import org.ourgrid.acceptance.util.discoveryservice.Req_502_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_504_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_505_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_506_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_508_Util;
import org.ourgrid.discoveryservice.DiscoveryServiceComponent;
import org.ourgrid.reqtrace.ReqTest;

import br.edu.ufcg.lsd.commune.functionaltests.util.TestStub;
import br.edu.ufcg.lsd.commune.identification.ContainerID;
import br.edu.ufcg.lsd.commune.identification.ServiceID;


public class Req_508_Test extends DiscoveryServiceAcceptanceTestCase {
	
	Req_502_Util req_502_Util = new Req_502_Util(getComponentContext());
	Req_504_Util req_504_Util = new Req_504_Util(getComponentContext());
	Req_505_Util req_505_Util = new Req_505_Util(getComponentContext());
	Req_506_Util req_506_Util = new Req_506_Util(getComponentContext());
	Req_508_Util req_508_Util = new Req_508_Util(getComponentContext());
	
	/**
	 * Create a DS;
     * Start a DS with the correct public key;
     * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test, servername = servertest,service = REMOTE_WORKERPROVIDER and publickey = dsClientPK;
          o PeerStatusProvider: username = test, servername = servertest,service = PEER and publickey = dsClientPK;
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list contains the peer added.
     * Call the doNotifyFailure message with the deploymentID:
          o username = test, servername = servertest and service = DS_CLIENT and publickey = dsClientPK;
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list is empty.
	 * @throws Exception
	 */
	@ReqTest(test = "AT-508.1", reqs = "")
	@Test public void test_AT_508_1_FailureNotification() throws Exception{
		// Start the Discovery Service
		DiscoveryServiceComponent component = req_502_Util.startDiscoveryService();
		
		// Create a WorkerProvider ServiceID
		TestStub dscTestStub = req_505_Util.createDiscoveryServiceClient(new ServiceID(new ContainerID("test", "servertest", "PEER", "dsClientPK"), "REMOTE_WORKERPROVIDER"));
		
		// Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub);
		
		// Call getCompleteStatus message
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub.getDeploymentID().getContainerID().getUserAtServer());
		req_504_Util.getCompleteStatus(component, dscServiceIDList);
		
		// Call doNotifyFailure message
		req_508_Util.doNotifyFailure(component, dscTestStub);
		
		// Call getCompleteStatus message
		req_504_Util.getEmptyCompleteStatus(component);
	}
	
	/**
	 * Create a DS;
     * Start a DS with the correct public key;
     * Call the joinCommunity message with the following parameters: 
          o WorkerProvider: username = test, servername = servertest,service = REMOTE_WORKERPROVIDER and publickey = dsClientPK;
          o PeerStatusProvider: username = test, servername = servertest,service = PEER and publickey = dsClientPK;
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list contains the peer added.
     * Call the doNotifyFailure message with the deploymentID:
          o username = test, servername = servertest and service = DS_CLIENT and publickey = dsClientPK;
     * Call the leaveCommunity message with the id of the peer added;
     * Verify if the following warn message was logged:
         1. The client with ID [deploymentID] is not joined to the community.
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list is empty.
	 * @throws Exception
	 */
	@ReqTest(test = "AT-508.2", reqs = "")
	@Test public void test_AT_508_2_FailureNotificationForARemovedClient() throws Exception{
		// Start the Discovery Service
		DiscoveryServiceComponent component = req_502_Util.startDiscoveryService();
		
		// Create a WorkerProvider ServiceID
		TestStub dscTestStub = req_505_Util.createDiscoveryServiceClient(new ServiceID(new ContainerID("test", "servertest", "PEER", "dsClientPK"), "REMOTE_WORKERPROVIDER"));
		
		// Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub);
		
		// Call getCompleteStatus message
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub.getDeploymentID().getContainerID().getUserAtServer());
		req_504_Util.getCompleteStatus(component, dscServiceIDList);
		
		// Call doNotifyFailure message
		req_508_Util.doNotifyFailure(component, dscTestStub);
		
		// Call getCompleteStatus message
		req_504_Util.getEmptyCompleteStatus(component);
	}
	
	/**
     * Create a DS;
     * Start a DS with the correct public key;
     * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test, servername = servertest,service = REMOTE_WORKERPROVIDER and publickey = dsClientPK;
          o PeerStatusProvider: username = test, servername = servertest,service = PEER and publickey = dsClientPK;
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list contains the peer added.
     * Call the doNotifyFailure message with the deploymentID set to null.
     * Verify if the following warn message was logged:
         1. Client ID invalid: null.
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list contains the Peer added.
	 * @throws Exception
	 */
	@ReqTest(test = "AT-508.3", reqs = "")
	@Test public void test_AT_508_3_FailureNotificationWithNullMonitorableID() throws Exception{
		// Start the Discovery Service
		DiscoveryServiceComponent component = req_502_Util.startDiscoveryService();
		
		// Create a WorkerProvider ServiceID
		TestStub dscTestStub = req_505_Util.createDiscoveryServiceClient(new ServiceID(new ContainerID("test", "servertest", "PEER", "dsClientPK"), "REMOTE_WORKERPROVIDER"));
		
		// Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub);
		
		// Call getCompleteStatus message
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub.getDeploymentID().getContainerID().getUserAtServer());
		req_504_Util.getCompleteStatus(component, dscServiceIDList);
		
		// Call doNotifyFailure message with null deploymentID
		req_508_Util.doNotifyFailureWithNullID(component, dscTestStub);
		
		// Call getCompleteStatus message
		req_504_Util.getCompleteStatus(component, dscServiceIDList);
	}
	
	
}
